package com.canal.collection.handler;

import com.canal.collection.exception.BuzException;
import com.canal.collection.result.Result;
import com.canal.collection.result.ResultUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.validation.ValidationException;

/**
 * 异常处理
 * @author yangbo
 * @date 2020-06-23 14:49
 */
@ControllerAdvice
public class BuzExceptionHandler {

    /**
     * 处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler({BuzException.class})
    public ResponseEntity<Object> warehouseExceptionHandler(Exception e) {
        BuzException be = (BuzException) e;
        return ResponseEntity.ok().body(ResultUtil.makeFailure(be.getCode(),be.getMessage()));
    }

    /**
     * 请求方法错误
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ResponseBody
    public Result methodNotAllowedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        return ResultUtil.makeFailure(HttpStatus.METHOD_NOT_ALLOWED.value(),"当前方法不支持");
    }

    /**
     * 不支持当前媒体类型错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public Result handlerHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return ResultUtil.makeFailure(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(),"不支持当前媒介类型");
    }

    /**
     * 参数未呈现
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestPartException.class)
    @ResponseBody
    public Result handlerMissingServletRequestPartException(MissingServletRequestPartException e) {
        return ResultUtil.makeFailure(HttpStatus.BAD_REQUEST.value(),"错误的请求");
    }

    /**
     * 客户端的数据解析错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Result handleMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        return ResultUtil.makeFailure(HttpStatus.BAD_REQUEST.value(),"客户端数据不可读取");
    }

    /**
     * 参数校验错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ValidationException.class)
    @ResponseBody
    public Result handlerValidateException(ValidationException e) {
        return ResultUtil.makeFailure(HttpStatus.BAD_REQUEST.value(),"参数校验出错");
    }


    /**
     * 参数校验错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return ResultUtil.makeFailure(HttpStatus.BAD_REQUEST.value(),"参数校验出错");
    }
}
